package org.edu.modules.category.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.config.pojo.POJOUtils;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.system.query.QueryGenerator;
import org.edu.modules.category.entity.VeCuzCategory;
import org.edu.modules.category.service.IVeCuzCategoryService;
import org.edu.modules.category.vo.VeCuzCategoryBo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@Api(tags={"课程分类相关信息"})
@RestController
@RequestMapping({"/course/veCuzCategory"})
public class VeCuzCategoryController
        extends BaseController<VeCuzCategory, IVeCuzCategoryService>
{
    private static final Logger log = LoggerFactory.getLogger(VeCuzCategoryController.class);
    @Autowired
    private IVeCuzCategoryService veCuzCategoryService;

    @AutoLog("ve_cuz_category-分页列表查询")
    @ApiOperation(value="ve_cuz_category-分页列表查询", notes="ve_cuz_category-分页列表查询")
    @PostMapping({"/list"})
    public Result<?> queryPageList(VeCuzCategory veCuzCategory, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req)
    {
        QueryWrapper<VeCuzCategory> queryWrapper = QueryGenerator.initQueryWrapper(veCuzCategory, req.getParameterMap());
        queryWrapper.orderByAsc("listSort");
        Page<VeCuzCategory> page = new Page(pageNo.intValue(), pageSize.intValue());
        IPage<VeCuzCategory> pageList = this.veCuzCategoryService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @AutoLog("ve_cuz_category-添加")
    @ApiOperation(value="ve_cuz_category-添加", notes="ve_cuz_category-添加")
    @PostMapping({"/add"})
    public Result<?> add(@RequestBody VeCuzCategory veCuzCategory)
    {
        this.veCuzCategoryService.save(veCuzCategory);
        return Result.OK("添加成功！");
    }

    @AutoLog("ve_cuz_category-编辑")
    @ApiOperation(value="ve_cuz_category-编辑", notes="ve_cuz_category-编辑")
    @PostMapping({"/edit"})
    public Result<?> edit(@RequestBody VeCuzCategory veCuzCategory)
    {
        this.veCuzCategoryService.updateById(veCuzCategory);
        return Result.OK("编辑成功!");
    }

    @AutoLog("ve_cuz_category-通过id删除")
    @ApiOperation(value="ve_cuz_category-通过id删除", notes="ve_cuz_category-通过id删除")
    @PostMapping({"/delete/{id}"})
    public Result<?> delete(@PathVariable String id)
    {
        this.veCuzCategoryService.removeById(id);
        return Result.OK("删除成功!");
    }

    @AutoLog("ve_cuz_category-批量删除")
    @ApiOperation(value="ve_cuz_category-批量删除", notes="ve_cuz_category-批量删除")
    @PostMapping({"/deleteBatch"})
    public Result<?> deleteBatch(@RequestBody List<Integer> ids)
    {
        this.veCuzCategoryService.removeByIds(ids);
        return Result.OK("批量删除成功!");
    }

    @AutoLog("ve_cuz_category-通过id查询")
    @ApiOperation(value="ve_cuz_category-通过id查询", notes="ve_cuz_category-通过id查询")
    @PostMapping({"/queryById"})
    public Result<?> queryById(@RequestParam(name="id", required=true) String id)
    {
        VeCuzCategory veCuzCategory = (VeCuzCategory)this.veCuzCategoryService.getById(id);
        if (veCuzCategory == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(veCuzCategory);
    }

    @RequestMapping({"/exportXls"})
    public ModelAndView exportXls(HttpServletRequest request, VeCuzCategory veCuzCategory)
    {
        return super.exportXls(request, veCuzCategory, VeCuzCategory.class, "ve_cuz_category");
    }

    @RequestMapping(value={"/importExcel"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response)
    {
        return super.importExcel(request, response, VeCuzCategory.class);
    }

    @ApiOperation(value="课程分类数", notes="分类数据")
    @PostMapping({"/categoryList"})
    public Result<?> categoryList()
    {
        List<VeCuzCategory> list = this.veCuzCategoryService.list((Wrapper)new QueryWrapper().orderByAsc("listSort"));

        List<VeCuzCategoryBo> bos = POJOUtils.listCopyProperties(list, VeCuzCategoryBo.class);

        List<VeCuzCategoryBo> allMenuTree = new ArrayList();
        Iterator localIterator1;
        if (bos != null) {
            for (localIterator1 = bos.iterator(); localIterator1.hasNext();)
            {
                VeCuzCategoryBo bo = (VeCuzCategoryBo)localIterator1.next();
                if (bo.getPid().intValue() == 0) {
                    allMenuTree.add(bo);
                }
                for (VeCuzCategoryBo veCuzCategoryBo : bos) {
                    if (veCuzCategoryBo.getPid().equals(bo.getId()))
                    {
                        if (bo.getChildren() == null) {
                            bo.setChildren(new ArrayList());
                        }
                        bo.getChildren().add(veCuzCategoryBo);
                    }
                }
            }
        }
        VeCuzCategoryBo bo;
        return Result.OK(allMenuTree);
    }
}
